<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script>
        function getItem(){
            var idArray = [];
            //得到所有选中复选框
            var boxList = document.querySelectorAll(
                "#roleDiv [name='roleArray']:checked");
            for(var i=0;i<boxList.length;i++){
                idArray.push(boxList[i].value);
            }
            alert(idArray);
        }

        function setItem(){
            var arr = [1,3,4];
            for(var i=0;i<arr.length;i++){
                document.querySelector(
                    "#roleDiv [name='roleArray'][value='"+arr[i]+"']")
                    .checked = true;
            }
        }
    </script>
</head>
<body>
    拥有角色：<div id="roleDiv">
        <input type="checkbox" name="roleArray" value="1">普通用户
        <input type="checkbox" name="roleArray" value="2">业务员
        <input type="checkbox" name="roleArray" value="3">经理
        <input type="checkbox" name="roleArray" value="4">老板
    </div>
    <input type="button" value="得到选中项" onclick="getItem()">
    <input type="button" value="设置选中项" onclick="setItem()">
</body>
</html>